
//tab
var listTab=[
    {
        "lights":false,
        "toHref":'overyears.html',
        "cont":'真题模拟'
    },
    {
        "lights":false,
        "toHref":'special.html',
        "cont":'专项练习'
    },
    // {
    //     "lights":false,
    //     "toHref":'javascript:void(0)',
    //     "cont":'智能模考'
    // },
    // {
    //     "lights":false,
    //     "toHref":'javascript:void(0)',
    //     "cont":'错题分析'
    // },
    {
        "lights":false,
        "toHref":'screen.html',
        "cont":'做题历史'
    }
];

//默认历史真题tp
var qctpToTp=function(){
    var arr2={
        'qctpType':'tp'
    };
    $.cookie('qctpBool',JSON.stringify(arr2));
};

//tab on=1:历史真题，2:专项练习，3：智能模考，4：错题分析
var listTabEvent=function(on){
    if(on>0){
        listTab[on-1].lights=true;
    }
    $(".tabListWrap").empty();
    for(var i=0;i<listTab.length;i++){
        var lights=listTab[i].lights?'tabList on':'tabList';
        var toHref=listTab[i].toHref;
        var cont=listTab[i].cont;
        var li='<div class="'+lights+'"><a href="'+toHref+'">'+cont+'</a><i></i></div>';
        $(".tabListWrap").append(li)
    }
    $(".tabList").unbind('click').click(function(){
        qctpToTp()//默认历史真题tp
    })
};

//清空cookie
var clearAllCookie=function() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if(keys) {
        for(var i = keys.length; i--;)
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
    }
};

//设置cookie
var setCookie=function(key,val){
    var expire= new Date();
    expire.setTime(expire.getTime() + (4*60*60 * 1000));
    $.cookie(key,val,{expires:expire});
};

//登出
$(".loginOutBut").click(function(){
    $.ajax({
        type: "POST",
        url:base_url + "logout",
        data:{"auth_token":$.cookie('auth_token')},
        beforeSend:function(XMLHttpRequest){

        },
        success:function(data){
            console.log(data);
            if(data.is_success){
                setTimeout(function(){
                    setUrl('noipWeb','noipWeb/login')//设置跳转
                },1000)
            }
        },
        error:function(){
            console.log('失败')
        }
    });
});

//echart
var echartEvent=function(data){
    var datas=data;
    var myChart = echarts.init(document.getElementById('main'));
    option = {// 指定图表的配置项和数据
        xAxis: {
            type: 'category',
            data:datas.dataX ,
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            splitNumber: 2
        },
        yAxis: {
            type: 'value',
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            splitNumber: 2,
            offset: -20,
            axisLabel: {
                formatter: function (value) {
                    var dataVal = value / 100;
                    return dataVal + '%'
                },
            }
        },
        series: [{
            data:datas.dataY ,
            type: 'line',
            itemStyle: {
                normal: {
                    color: '#7bd2c6'
                }
            },
            areaStyle: {
                normal: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#6bcdc0' // 0% 处的颜色
                        }, {
                            offset: 0.5, color: '#fff' // 100% 处的颜色
                        }, {
                            offset: 1, color: '#fff' // 100% 处的颜色
                        }],
                        globalCoord: false // 缺省为 false
                    }
                }
            },
        }]
    };
    myChart.setOption(option);
};

//设置跳转URL
var setUrl=function(from,toUrl){
    var url = document.location.toString();
    var start = url.indexOf("/"+from);
    var relUrl = url.substr(0,start+1);//stop省略，截取从start开始到结尾的所有字符
    window.location=relUrl+toUrl+'.html'
};

//秒转时分秒
function formatSeconds(value) {
    var theTime = parseInt(value);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    if(theTime > 60) {
        theTime1 = parseInt(theTime/60);
        theTime = parseInt(theTime%60);
        if(theTime1 > 60) {
            theTime2 = parseInt(theTime1/60);
            theTime1 = parseInt(theTime1%60);
        }
    }
    var result = "00:00:"+(theTime>9?theTime:'0'+theTime);
    if(theTime1 > 0) {
        result = "00:"+(theTime1>9?theTime1:'0'+theTime1)+":"+(theTime>9?theTime:'0'+theTime);
    }
    if(theTime2 > 0) {
        result = (theTime2>9?theTime2:'0'+theTime2)+":"+(theTime1>9?theTime1:'0'+theTime1)+":"+(theTime>9?theTime:'0'+theTime);
    }
    return result;
}

//提示
var layTxt=function(content){
    layui.use('layer', function(){
        var $ = layui.jquery, layer = layui.layer;
        layer.open({
            content: content
            ,skin: 'msg'
            ,time: 1000 //2秒后自动关闭
        });
    })
};

//判断对象为空
var isEmptyObject=function(obj) {
    for (var key in obj){
        return false;//返回false，不为空对象
    }
    return true;//返回true，为空对象
};

//填空题答案
var editorGet=function(){
    var editorTxt=$("input[name='editorTxt']");
    if(editorTxt.length>0){
        var editorObject=new Object();
        for(var r=0;r<editorTxt.length;r++){
            if(editorTxt.eq(r).val()!==''){
                editorObject[r+1]=editorTxt.eq(r).val();
            }
        }
        if(!isEmptyObject(editorObject)){
            return JSON.stringify(editorObject);
        }else{
            return ''
        }
    }
};

//剩余天数
var dateDayEvent=function(){
    var laseDateTxt='2019-10-13';
    var startDate = new Date();
    // Date.prototype.toLocaleString = function() {
    //     // return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
    //     return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
    // };
    var laseDate=new Date(laseDateTxt);
    var allDate=(parseInt(laseDate-startDate)/parseInt(24*60*60*1000)).toFixed(0).toString();
    console.log(parseInt(laseDate-startDate)/parseInt(24*60*60*1000));
    console.log(allDate)

    var pushTxtArray=allDate.split('');
    console.log(pushTxtArray)
    if(pushTxtArray.length>0){
        var dateTxt='';
        for(var i=0;i<pushTxtArray.length;i++){
            dateTxt+='<div class="dateDayList">\n' +
                '                            <div class="dateDay on" data-day="'+pushTxtArray[i]+'" style="background-image:url(\'images/'+pushTxtArray[i]+'.png\')"></div>\n' +
                '                        </div>\n'
        }
        dateTxt+= '                        <div class="dateDayList">\n' +
            '                            <div class="dateTime">天</div>\n' +
            '                        </div>'
    }
    $(".dateDayWrap").html(dateTxt);
};
dateDayEvent();

//判断is_success为false或者请求失败返回上一页
var successFalse=function(data){
    layTxt(data);
    setTimeout(function(){
        window.history.go(-1);
    },2000);
};